//
//  secondViewController.swift
//  TestSwift
//
//  Created by elarc on 16/4/25.
//  Copyright © 2016年 elarc. All rights reserved.
//

import Foundation
import UIKit

class secondViewController: UIViewController,UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning {
    var isPersent:Bool?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.whiteColor()
        let btn = UIButton(type: .Custom)
        btn.frame = CGRectMake(100, 100, 100, 40)
        btn.backgroundColor = UIColor.redColor()
        view.addSubview(btn)
        btn.addTarget(self, action: "modalto", forControlEvents: UIControlEvents.TouchUpInside)
    }
    
    func modalto () {
        dismissViewControllerAnimated(true) { () -> Void in
            
        }
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
    }
 
        func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning?{
            //转场
            isPersent = true
            //源控制器自己负责转场
            return self
        }
        
        func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?{
            
            //退场
            isPersent = false
            //源控制器自己负责转场
            return self
        }
        
        func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval{
            return 0.5
        }
        
        //转场上下文，负责转场动画的具体内容
        func animateTransition(transitionContext: UIViewControllerContextTransitioning){
            //转场动画
            if (isPersent != nil){
                if let toView = transitionContext.viewForKey(UITransitionContextToViewKey){
                    //将傀儡视图（用于动画时展现的画面）加入containerView()
                    transitionContext.containerView()!.addSubview(self.view!)
                    toView.frame = CGRectZero
                    self.view?.frame = CGRectZero
                    
                    UIView.animateWithDuration(2.0, animations: { () -> Void in
                        self.view?.frame = transitionContext.containerView()!.frame
                        toView.frame = UIScreen.mainScreen().bounds
                        }, completion: { (_) -> Void in
                            //移除傀儡视图
                            self.view?.removeFromSuperview()
                            //加入要正常显示的视图
                            transitionContext.containerView()!.addSubview(toView)
                            //结束
                            transitionContext.completeTransition(true)
                    })
                }
            }
            //退场动画
            if (isPersent == nil){
                if let _ = transitionContext.viewForKey(UITransitionContextFromViewKey){
                    
                    //隐藏目标控制器
                    let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey) as? secondViewController
                    fromVC!.view.hidden = true
                    //获得fromVC的快照
                    let dummy = fromVC?.view
                    //                                 //将傀儡视图放入containerView
                    transitionContext.containerView()!.addSubview(dummy!)
                    //执行动画
                    UIView.animateWithDuration(2.0, animations: { () -> Void in
                        dummy!.frame = CGRectZero
                        }, completion: { (_) -> Void in
                            //移除傀儡视图
                            dummy!.removeFromSuperview()
                            //结束
                            transitionContext.completeTransition(true)
                    })
                }
            }
        }
        
        
    }
